
/*
7 avr. 2012 jblanchard 
*/

package org.jcb.service;

import java.util.ArrayList;
import java.util.List;

import org.jcb.dao.data.StrategyData;
import org.jcb.model.ChineseQuestionnaire;
import org.jcb.model.DataChineseLanguage;

import android.util.Log;




public class GenerateQuestionnaireRandomStrategy {

public static ChineseQuestionnaire generate(List<DataChineseLanguage> base,  StrategyData strategyData) {
	ChineseQuestionnaire cq = new ChineseQuestionnaire(strategyData.isNeverEnding());
	List<DataChineseLanguage> data = extractData(base,strategyData);
	if (data.size() < strategyData.getNbDataExtact() ) {
		throw new RuntimeException("Trop de question" + " max = " + data.size());
	}
	 Log.d("TAG", "data.size()" + data.size() + "  " + strategyData.getNbDataExtact());
	
	
	for (int i = 0 ; i < strategyData.getNbDataExtact() ; i++ ) {
		int index = 0 ;
		if (strategyData.isRandom()) {
			index = (int) (Math.random() *  data.size()) ;
			//System.out.println("random=" + random);
		}
		else {
			//index = i ;	
		}
		cq.addQuestion(data.get(index)) ;
		data.remove(index);
	}
	return cq ;
}

protected static List<DataChineseLanguage> extractData(List<DataChineseLanguage> data,StrategyData strategyData) {
	List<DataChineseLanguage> extract = new ArrayList<DataChineseLanguage>();
	for (DataChineseLanguage dcl : data) {
		//if (filter.equals(dcl.getTagQuestion())) {
		if (strategyData.isLevel1()  && dcl.isFirstLevel()) {
			extract.add(dcl);
		}
		//si on recherche par tag on test
		if (
			(strategyData.getNumQuizz().length() > 0  && strategyData.getNumQuizz().equals(dcl.getNumQuizz()) ) 
		&& (strategyData.getIdTypeQuizz() == null || (dcl.getIdTypeQuizz() != null &&  strategyData.getIdTypeQuizz().intValue() == dcl.getIdTypeQuizz().intValue() ))		
				) {
			extract.add(dcl);
		}
		
	}
	return extract ;
}


}
